import router from "./router";
import store from "./store";
import NProgress from "nprogress"; // Progress 进度条
import "nprogress/nprogress.css"; // Progress 进度条样式
import { Message } from "element-ui";
import { getToken } from "@/utils/auth"; // 验权

import Cookies from "js-cookie";

const whiteList = ["/login"]; // 不重定向白名单
router.beforeEach((to, from, next) => {
  NProgress.start();

  //把上级ID写到cookie
  let cpid = Cookies.get("pid");
  //let pid = cpid && cpid != "0" ? cpid : to.query.pid;
  let pid = to.query.pid;
  pid = typeof pid === "undefined" || pid == "" ? false : pid;
  pid = !pid && cpid ? cpid : pid;
  pid = !pid ? "0" : pid;
  Cookies.set("pid", pid);

  console.log(pid, to.query);

  let openid = to.query.openid;
  openid = typeof openid === "undefined" || openid == "" ? "" : openid;
  if (openid) {
    if (typeof openid === "object") {
      openid = openid[0];
    }
    console.log(typeof openid, openid);
    console.log("befor---------------------------------");
    Cookies.set("openid", openid);
  }

  if (getToken()) {
    if (to.path === "/login") {
      next({ path: "/?pid=" + pid });
      NProgress.done(); // if current page is dashboard will not trigger	afterEach hook, so manually handle it
    } else {
      if (store.getters.roles.length === 0) {
        store
          .dispatch("GetInfo")
          .then(res => {
            // 拉取用户信息
            next();
          })
          .catch(err => {
            store.dispatch("FedLogOut").then(() => {
              Message.error(err || "Verification  failed, please login again");
              next({ path: "/?pid=" + pid });
            });
          });

        // store
        //   .dispatch("GetInfo")
        //   .then(res => {
        //     // 拉取info
        //     const roles = res.data.roles;
        //     //console.log(res, roles);
        //     store.dispatch("GenerateRoutes", { roles }).then(() => {
        //       // 生成可访问的路由表
        //       router.addRoutes(store.getters.addRouters); // 动态添加可访问路由表
        //       next({ ...to, replace: true }); // hack方法 确保addRoutes已完成 ,set the replace: true so the navigation will not leave a history record
        //     });
        //   })
        //   .catch(err => {
        //     console.log(err);
        //   });
      } else {
        next();
      }
    }
  } else {
    if (whiteList.indexOf(to.path) !== -1) {
      next();
    } else {
      next("/login?pid=" + pid);
      NProgress.done();
    }
  }
});

router.afterEach(to => {
  NProgress.done(); // 结束Progress

  //console.log(to.query, getToken());
  if (getToken()) {
  } else {
    let openid = to.query.openid;
    openid = typeof openid === "undefined" || openid == "" ? "" : openid;
    if (openid) {
      if (typeof openid === "object") {
        openid = openid[0];
      }
      console.log(typeof openid, openid);
      console.log("---------------------------------");
      Cookies.set("openid", openid);
    }

    openid = Cookies.get("openid");

    let pid = to.query.pid;
    pid = typeof pid === "undefined" || pid == "" ? "0" : pid;

    console.log(typeof openid, openid);
    console.log("---------------------------------");

    //console.log(openid);

    var ua = window.navigator.userAgent.toLowerCase();
    if (ua.match(/MicroMessenger/i) == "micromessenger") {
      if (openid) {
        //注册
        console.log("注册");
        store
          .dispatch("wxLogin", { openid: openid, pid: pid })
          .then(re => {
            console.log(re);
            if (typeof re.token !== "undefined" && re.token) {
              window.location = "/";
            }
          })
          .catch(() => {});

        return;
      } else {
        console.log("getopenid");
        if (to.query.over) {
          return;
        }
        window.location =
          "http://m.tripg.com/MO/?url=" +
          encodeURIComponent(window.location.href + "&over=1");
        return;
      }
    }
  }
});
